<?php
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Auth
 *
 * @author Administrator
 */
class Auth {
    
    private $access_token;
    protected $logger;
    protected $client;
    
    // We'll use a constructor, as you can't directly call a function
    // from a property definition.
    public function __construct($logger,$redis) {
      $this->client = $redis;
      $this->logger = $logger;
    }
    
    //put your code here
    public function getToken($userId){
        $str =$this->rand(8);
        $kdy = $userId.$str;
        $md5 = md5($str);
        $base64 = base64_encode($md5);
        $this->access_token = $base64;
        return $this->access_token;
    }
    
    private function rand($len)
    {
        $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
        $string=time();
        for(;$len>=1;$len--)
        {
            $position=rand()%strlen($chars);
            $position2=rand()%strlen($string);
            $string=substr_replace($string,substr($chars,$position,1),$position2,0);
        }
        return $string;
    }
       public function setAuth($access_token,$userId){
          //保存App用户登录状态
        $this->logger->info("保存App用户登录状态");
        $res = $this->client->set($access_token,$userId);
        $this->logger->info("保存App用户登录状态". json_encode($res));
         return $res;
    }
         public function getAuth($access_token){
          //保存App用户登录状态
            $this->logger->info("获取App用户登录状态");
            $res = $this->client->get($access_token);
            $this->logger->info("获取App用户登录结果". json_encode($res));
            return $res;
    }
    
    public function clearAuth($access_token){
          $this->logger->info("清除用户登录状态");
           $res = $this->client->exists  ($access_token);
           $ret = $this->client -> del ( $access_token ) ; //true
           $ret = true;
          $this->logger->info("清除用户登录状态". json_encode($ret));
          return $ret;
    }
}
